﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;

namespace EIA.SLClient.ProjectManage
{
    public class ChangePasswordViewModel : SimpleMvvmToolkit.ViewModelBase<ChangePasswordViewModel>
	{
		public ChangePasswordViewModel()
		{
			IsShow = true;

            this.RegisterToReceiveMessages(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.CHANGE_PASSWORD, EP_ChangePassword);
		}
        private void EP_ChangePassword(object sender, SimpleMvvmToolkit.NotificationEventArgs e)
        {
            IsShow = true;
        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected string _TitleTip;
        /// <summary>
        /// 注释
        /// </summary>
        public string TitleTip
        {
            get { return _TitleTip; }
            set
            {
                _TitleTip = value;
                NotifyPropertyChanged(m => m.TitleTip);
            }
        }

        protected bool _IsShow;
        /// <summary>
        /// 是否显示
        /// </summary>
        public bool IsShow
        {
            get { return _IsShow; }
            set
            {
                _IsShow = value;
                NotifyPropertyChanged(m => m.IsShow);
            }
        }

        protected string _LoginId;
        /// <summary>
        /// 注释
        /// </summary>
        public string LoginId
        {
            get { return _LoginId; }
            set
            {
                _LoginId = value;
                NotifyPropertyChanged(m => m.LoginId);
            }
        }

        protected string _OldPassword;
        /// <summary>
        /// 注释
        /// </summary>
        public string OldPassword
        {
            get { return _OldPassword; }
            set
            {
                _OldPassword = value;
                NotifyPropertyChanged(m => m.OldPassword);
            }
        }

        protected string _New0Password;
        /// <summary>
        /// 注释
        /// </summary>
        public string New0Password
        {
            get { return _New0Password; }
            set
            {
                _New0Password = value;
                NotifyPropertyChanged(m => m.New0Password);
            }
        }

        protected string _New1Password;
        /// <summary>
        /// 注释
        /// </summary>
        public string New1Password
        {
            get { return _New1Password; }
            set
            {
                _New1Password = value;
                NotifyPropertyChanged(m => m.New1Password);
            }
        }

        #endregion

        
        #region SaveCommand

        private SimpleMvvmToolkit.DelegateCommand _SaveCommand = null;
        public SimpleMvvmToolkit.DelegateCommand SaveCommand
        {
            get
            {
                if (_SaveCommand == null)
                {
                    _SaveCommand = new SimpleMvvmToolkit.DelegateCommand(Save, CanExcuteSave);
                } return _SaveCommand;
            }
            set { _SaveCommand = value; NotifyPropertyChanged(m => m.SaveCommand); }
        }

        public void Save()
        {
            if(string.IsNullOrEmpty(LoginId))
            {
                TitleTip = "登录名未填";
                return;
            }
            else if (string.IsNullOrEmpty(OldPassword))
            {
                TitleTip = "旧密码未填";
                return;
            }
            else if (string.IsNullOrEmpty(New0Password))
            {
                TitleTip = "新密码未填";
                return;
            }
            else if (New0Password != _New1Password)
            {
                TitleTip = "新密码不一致";
                return;
            }
            else
            {
                EIA.WCFService.AuthProxy.AuthenticationProxy proxy = new WCFService.AuthProxy.AuthenticationProxy();

                Busy("提交修改密码！");
                proxy.ChangePassword(LoginId, OldPassword, New0Password, (r) =>
                {
                    Free();
                    if (r)
                    {
                        IsShow = false;
                    }
                    else
                    {
                        TitleTip = "修改失败";
                    }
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }
        }

        public bool CanExcuteSave()
        {
            return true;
        }

        #endregion

        #region CancelCommand

        private SimpleMvvmToolkit.DelegateCommand _CancelCommand = null;
        public SimpleMvvmToolkit.DelegateCommand CancelCommand
        {
            get
            {
                if (_CancelCommand == null)
                {
                    _CancelCommand = new SimpleMvvmToolkit.DelegateCommand(Cancel, CanExcuteCancel);
                } return _CancelCommand;
            }
            set { _CancelCommand = value; NotifyPropertyChanged(m => m.CancelCommand); }
        }

        public void Cancel()
        {
            IsShow = false;
        }

        public bool CanExcuteCancel()
        {
            return true;
        }

        #endregion

    }
}